# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 John Paulett (john -at- vanderbilt.edu)
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

"""GUI API that provides a core set of widgets that form the user interface 
of HORNET.
"""

import wx
import wx.grid
import wx.py 
from wx.lib.wordwrap import wordwrap

import hornet.release as release

class MainWindow(wx.Frame):
    def __init__(self,  *args, **kw):
        wx.Frame.__init__(self,  *args, **kw)
        self.SetMinSize((600, 480))
        
        self._create_menubar()
        
        self.window = wx.SplitterWindow(self, -1, style=wx.SP_3D|wx.SP_BORDER)
        
        self.notebook = wx.Notebook(self.window, -1, style=0)
        self.grid = wx.grid.Grid(self.notebook, -1, size=(1, 1))
        self.grid.CreateGrid(10, 3)
        self.shell = wx.py.shell.Shell(self.window, introText='HORNET Python Shell') 
        #self.shell = wx.py.crust.Crust(self, intro='HORNET Python Shell')
        #self.shell.SetMinSize((300, 280))
        #self.grid = DataGridWidget(self)
        self.__do_layout()
        
    def about_dialog(self, event):
        info = wx.AboutDialogInfo()
        info.Name = release.proper_name
        info.Version = release.version
        info.Copyright = release.copyright
        info.Description = wordwrap(
            release.long_description,
            350, wx.ClientDC(self))
        info.WebSite = (release.url, "HORNET project site")
        info.Developers = [ 'John Paulett (john.paulett@vanderbilt.edu)' ]

        info.License = wordwrap(release.license, 500, wx.ClientDC(self))

        wx.AboutBox(info)
        
    def close_window(self, event):
        self.Close()    
    
    def _create_menubar(self):
        self.menubar = wx.MenuBar()

        menu1 = wx.Menu()
        menu1.AppendSeparator()
        menu1.Append(101, '&Close', 'Close this HORNET')
        self.menubar.Append(menu1, '&File')
        
        menu2 = wx.Menu()
        menu2.Append(201, '&About', 'Information about HORNET')
        self.menubar.Append(menu2, '&Help')

        self.Bind(wx.EVT_MENU, self.close_window, id=101)
        self.Bind(wx.EVT_MENU, self.about_dialog, id=201)

        self.SetMenuBar(self.menubar)

    def __do_layout(self):
        self.window.SplitHorizontally(self.notebook, self.shell)
        self.notebook.AddPage(self.grid, "tab 1")
        self.notebook.AddPage(wx.Panel(self.notebook, -1), "tab 2")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.window, 1, wx.EXPAND, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

########################
        
class AbstractWidget():
    pass

class DataGridTable(wx.grid.PyGridTableBase):
    # This is all it takes to make a custom data table to plug into a    # wxGrid.  There are many more methods that can be overridden, but
    # the ones shown below are the required ones.  This table simply
    # provides strings containing the row and column values.
    
    def GetNumberRows(self):
        return 10000
    def GetNumberCols(self):
        return 5
    def IsEmptyCell(self, row, col):
        return False
    def GetValue(self, row, col):
        return str( (row, col) )
    def SetValue(self, row, col, value):
        pass # read only

class DataGridWidget(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)

        table = DataGridTable()
        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.
        self.SetTable(table, True)
        
        
class PlotWidget(AbstractWidget):
    pass


class HornetApp(wx.App):
    def OnInit(self):
        self.frame = MainWindow(parent=None, 
                                title=release.proper_name + ' ' + release.version)
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True
    
if __name__ == '__main__':
    app = HornetApp()
    app.MainLoop()
    